﻿#region Opis pliku
//-----------------------------------------------------------------------------
// MainMenu.cs
// Autor: Jan Czarnowski
// 22.10.2012
//-----------------------------------------------------------------------------
#endregion

#region Using...

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using System.Collections.Generic;
using System;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa reprezentująca główne menu.
    /// </summary>
    public class MainMenu : GameScreen
    {
        #region pola

        Rectangle menuRect;
        Rectangle dragRect;
        Point dragClickDelta = Point.Zero;
        
        bool isBeingDragged = false;

        Color bgColor;
        Color daColor;
        Color bdColor;

        string title;

        // lista elementów menu
        List<MenuItem> itemList = new List<MenuItem>();

        #endregion

        #region inicjalizacja

        /// <summary>
        /// Konstruktor. Tworzy nowy obiekt MainMenu
        /// </summary>
        public MainMenu()
        {
            menuRect.Width = 200;
            menuRect.Height = 300;

            // todo: glowna klasa bazowa dla menu i wywolywanie w kazdej pochodnej konstruktora bazowej
            // z wlasnymi defaultowymi opcjami
            this.title = "Menu";
            this.bgColor = Color.Goldenrod * 0.7f; 
            this.daColor = Color.Gold;
            this.bdColor = Color.DarkGoldenrod;
        }

        public override void LoadContent()
        {
            Rectangle rectview = ScreenManager.Game.GraphicsDevice.Viewport.Bounds;
            menuRect.X = (int)(0.1 * rectview.Width);
            menuRect.Y = (int)(0.2 * rectview.Height);
            
            dragRect = menuRect;
            dragRect.Height = 20;

            int menuItemHeight = 30;
            int menuItemWidth = menuRect.Width;

            MenuItem startItem = new MenuItem("Start Game", new Rectangle(menuRect.Left, dragRect.Bottom, menuRect.Width, menuItemHeight), this);
            MenuItem optionsItem = new MenuItem("Options", new Rectangle(menuRect.Left, dragRect.Bottom + menuItemHeight, menuRect.Width, menuItemHeight), this);
            MenuItem quitItem = new MenuItem("Quit", new Rectangle(menuRect.Left, dragRect.Bottom + 2*menuItemHeight, menuRect.Width, menuItemHeight), this);

            itemList.Add(startItem);
            itemList.Add(optionsItem);
            itemList.Add(quitItem);
        }

        #endregion

        #region metody draw i update

        /// <summary>
        /// Rysowanie ekranu
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ExtendedSpriteBatch exSpriteBatch = ScreenManager.ExtendedSpriteBatch;

            // cień
            exSpriteBatch.FillRectangle(new Rectangle(menuRect.X + 10, menuRect.Y + 10, menuRect.Width, menuRect.Height), Color.Black * 0.1f);

            // glowne prostokaty
            exSpriteBatch.FillRectangle(menuRect, bgColor);
            exSpriteBatch.FillRectangle(dragRect, daColor);

            // rysowanie ramki
            exSpriteBatch.DrawLine(new Vector2(menuRect.Left, menuRect.Top), new Vector2(menuRect.Left, menuRect.Bottom), bdColor);
            exSpriteBatch.DrawLine(new Vector2(menuRect.Left, menuRect.Bottom), new Vector2(menuRect.Right, menuRect.Bottom), bdColor);
            exSpriteBatch.DrawLine(new Vector2(menuRect.Right, menuRect.Bottom), new Vector2(menuRect.Right, menuRect.Top), bdColor);
            exSpriteBatch.DrawLine(new Vector2(menuRect.Right, menuRect.Top), new Vector2(menuRect.Left, menuRect.Top), bdColor);
            exSpriteBatch.DrawLine(new Vector2(dragRect.Left, dragRect.Bottom), new Vector2(dragRect.Right, dragRect.Bottom), bdColor);

            // tytuł okna
            Vector2 position;
            position.X = dragRect.Center.X - ScreenManager.Font.MeasureString(title).X/2;
            position.Y = dragRect.Center.Y - ScreenManager.Font.MeasureString(title).Y/2;
            exSpriteBatch.DrawString(ScreenManager.Font, title, position, Color.Black);

            foreach (MenuItem item in itemList)
                item.Draw(exSpriteBatch);
        }

        /// <summary>
        /// Logika ekranu
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            foreach (MenuItem item in itemList)
            {
                item.Update(gameTime);

                if (item.IsPressed)
                {
                    switch (item.Text)
                    {
                        case "Start Game":
                            LoadingScreen.Load(ScreenManager, true, new GamePlayScreen(), new GamePlayGUI());
                            break;
                        case "Options":
                            // costam
                            break;
                        case "Quit":
                            ScreenManager.Game.Exit();
                            break;
                    }
                }
            }
        }

        #endregion

        #region metody publiczne

        /// <summary>
        /// Obsługa urządzeń wejścia
        /// </summary>
        /// <param name="input"></param>
        public override void HandleInput(InputState input, bool otherScreenHasFocus, ref bool eventHandled)
        {
            // wyjście z gry
            if (input.IsNewKeyPress(Keys.Escape))
                ScreenManager.Game.Exit();

            Point mousePos = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            // jeżeli kliknięcie nastąpiło w menu, niech tło już go nie obsluguje
            if (menuRect.Contains(mousePos) && input.IsNewMLeftButtonPressed())
                eventHandled = true;

            // czy rozpoczęto ciągnięcie okna?
            if (input.IsNewMLeftButtonPressed() && dragRect.Contains(mousePos))
            {
                isBeingDragged = true;
                dragClickDelta.X = mousePos.X - menuRect.X;
                dragClickDelta.Y = mousePos.Y - menuRect.Y;
            }

            // czy zakończono ciągnięcie okna
            if (input.WasMLeftButtonReleased())
                isBeingDragged = false;

            // jeżeli okno jest ciągnięte, aktualizuj pozycję
            if (isBeingDragged && viewport.Bounds.Contains(mousePos))
            {
                menuRect.X = mousePos.X - dragClickDelta.X;
                menuRect.Y = mousePos.Y - dragClickDelta.Y;
                dragRect.X = menuRect.X;
                dragRect.Y = menuRect.Y;
            }

            for(int i = 0; i < itemList.Count; ++i)
            {
                // ustawianie nowyh pozycji elementów
                itemList[i].Bounds = new Rectangle(menuRect.X, dragRect.Bottom + i * itemList[i].Bounds.Height , 
                                                    itemList[i].Bounds.Width, itemList[i].Bounds.Height);

                itemList[i].HandleInput(input);
            }
        }

        #endregion
    }
}
